package org.catdroid.bcnappsjam.barcelonaparticipa;

import org.catdroid.bcnappsjam.barcelonaparticipa.util.NfcUtil;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		if (!NfcUtil.nfcAvailable(this)) {
			Button boton = (Button) findViewById(R.id.home_btn_send);
			boton.setVisibility(View.VISIBLE);
			boton.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					try {
						Intent intent = new Intent("com.google.zxing.client.android.SCAN");
						intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
						startActivityForResult(intent, 0);
					} catch (Exception e) {
						// Toast.makeText(getBaseContext(),
						// "You must first install Spotify",
						// Toast.LENGTH_LONG).show();
						Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.zxing.client.android"));
						startActivity(i);
					}
				}
			});
		} else {
			Button boton = (Button) findViewById(R.id.home_btn_send);
			boton.setVisibility(View.GONE);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {
				String contents = intent.getStringExtra("SCAN_RESULT");
				String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
				Intent i = new Intent(this, SendingActivity.class);
				i.putExtra(SendingActivity.EXTRA_OPINIO_ID, contents);
				startActivity(i);
			} else if (resultCode == RESULT_CANCELED) {
				// Handle cancel
			}
		}
	}

	public void onHomeVote(View view) {
		Intent captureIntent = new Intent(this, WaitingNfcScan.class);
		startActivity(captureIntent);
	}
}